package com.steel.yfmall.cloud.assistant.zg.impl;

import com.steel.yfmall.cloud.assistant.dto.zg.result.ZgSO06Bean;
import com.steel.yfmall.cloud.assistant.goods.mapper.FrameworkContractInfoMapper;
import com.steel.yfmall.cloud.assistant.po.goods.FrameworkContractInfo;
import com.steel.yfmall.cloud.assistant.po.goods.FrameworkContractInfoExample;
import com.steel.yfmall.cloud.assistant.zg.ReceiveZGMessage;
import com.steel.yfmall.cloud.components.utils.base.APIMsgBean;
import com.steel.yfmall.cloud.components.utils.base.ResponseCode;
import com.steel.yfmall.cloud.components.utils.base.SOAStateBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 接收中冠 回传 数据
 *
 * @author jia chang bao
 * @date 2021/1/6 15:45
 */
@Service
@Slf4j
public class ReceiveZGMessageImpl implements ReceiveZGMessage {
    /**
     * SO06-合同状态变更信息通知
     *
     * @param zgSO06Bean 接收参数
     * @date 2021/1/6 15:37
     * @author jia chang bao
     */
    @Override
    public APIMsgBean<Boolean> ReceiveZGSo06Message(ZgSO06Bean zgSO06Bean) {
        APIMsgBean<Boolean> apiMsgBean = new APIMsgBean<>();
        Map<String, String> checkParam = new HashMap<>();
        //暂时注释，电商》中冠》兰格,过程中电商合同号会被改变，但后期如果以电商为主该电商合同号生效
        //checkParam.put("commerceContractNo", "电商合同号");
        checkParam.put("contractNo", "中冠合同号");
        checkParam.put("status", " 产销合同状态");
        checkParam.put("contractVer", " 版次");
        SOAStateBean soaStateBean = ResponseCode.checkApiParameter(checkParam, zgSO06Bean);
        if (soaStateBean == null || StringUtils.isNotBlank(soaStateBean.getMessage())) {
            log.debug("修改框架合同状态失败，需要中冠合同号、产销合同状态、版次，参数不全：" + zgSO06Bean.toString());
            apiMsgBean.setData(false);
            apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean(soaStateBean.getMessage()));
            return apiMsgBean;
        }
        //封装修改信息
        FrameworkContractInfo frameworkContractInfo = new FrameworkContractInfo();
        frameworkContractInfo.setContractNo(zgSO06Bean.getContractNo());
        frameworkContractInfo.setZgStatus(zgSO06Bean.getStatus());
        frameworkContractInfo.setContractVer(zgSO06Bean.getContractVer());
        //封装条件
        FrameworkContractInfoExample frameworkContractInfoExample = new FrameworkContractInfoExample();
        FrameworkContractInfoExample.Criteria criteria = frameworkContractInfoExample.createCriteria();
        criteria.andContractNoEqualTo(zgSO06Bean.getContractNo());
        criteria.andContractVerEqualTo(zgSO06Bean.getContractVer());
        Boolean update = frameworkContractInfoMapper.updateByExampleSelective(frameworkContractInfo, frameworkContractInfoExample) > 0 ? true : false;
        if (update) {
            log.debug("修改状态成功：" + zgSO06Bean.toString());
            apiMsgBean.setData(true);
            apiMsgBean.setSoaStateBean(ResponseCode.buildEnumSuccessSOAStateBean("修改状态成功！"));
            return apiMsgBean;
        }
        log.debug("修改状态失败：" + zgSO06Bean.toString());
        apiMsgBean.setData(false);
        apiMsgBean.setSoaStateBean(ResponseCode.buildEnumErrorSOAStateBean("修改状态失败！"));
        return apiMsgBean;
    }

    @Resource
    private FrameworkContractInfoMapper frameworkContractInfoMapper;

}
